﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using BADDGUI.Utils.SQLHelper;
using BADDGUI.Utils;

namespace BADDGUI.Utils.DTHelper
{
    public static class DTHandler
    {
        internal static string[][] getColList(DataTable dtTable)
        {
            string[][] strCols = new string[dtTable.Columns.Count][];
            for (int i = 0; i < dtTable.Columns.Count; i++)
			{
                strCols[i][0] = dtTable.Columns[i].ColumnName;
                strCols[i][1] = dtTable.Columns[i].DataType.ToString();
			}
            return strCols;
        }
    }

    public class DataRetriever
    {
        private string tableName;
        private string commasepcolumns;
        private string range;
        private List<object> rangeargs;
        private List<string> sortby;

        public DataRetriever(string TableName, 
                             string CommaSeparatedColumns, 
                             string Range, List<object> Rangeargs, 
                             List<string> SortBy)
        {
            this.tableName = TableName;
            this.commasepcolumns = CommaSeparatedColumns;
            this.range = Range;
            this.rangeargs = Rangeargs;
            this.sortby = SortBy;
        }

        public DataTable SupplyPageOfData(string ViewKey,int KeyValue,int rowsPerPage)
        {
            string orderby = sortby.ToString(", ");
            string sql = "SELECT TOP " + rowsPerPage + ' ' + commasepcolumns + ' ' +
                         "FROM " + tableName + ' ' + 
                         "WHERE [" + ViewKey + "] > " + KeyValue + ' ' +
                         (range == "" ? "" : "AND ") + range + ' ' +
                         (orderby == "" ? "" : "ORDER BY ") + orderby;

            DataTable table = BLCGlobalData.BASLOG.GetADataTable(sql);
            return table;
        }
    }
}

